package com.ruoyi.eam.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eam.complex.UpdateEamAllot;
import com.ruoyi.eam.complex.UpdateEamLoanBack;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.service.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 资产调拨Controller
 *
 * @author xl
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/allot")
public class EamAllotController extends BaseController
{
    @Autowired
    private IEamAllotHService eamAllotHService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IEamLocationService locationService;
    @Autowired
    private IEamTypeService eamTypeService;
    @Autowired
    private IEamAllotDService eamAllotDService;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询资产调拨列表
     */
    //@RequiresPermissions("eam:allot:list")
    @GetMapping("/list")
    public TableDataInfo list(EamAllotH eamAllotH)
    {
        startPage();
        List<EamAllotH> list = eamAllotHService.selectEamAllotHList(eamAllotH);
        return getDataTable(list);
    }

    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("system:loanBack:typeList")
    @GetMapping("/typeList")
    public Map<String,Object> typeList(EamChgH eamChgH)
    {
        Map<String,Object> rm = new HashMap<>();
        startPage();
        List<EamType> typeList = eamTypeService.selectEamTypeList(null);
        List<EamLocation> locations = locationService.selectEamLocationList(null);
        List<Dept> depts = deptService.selectDeptList(null);
        rm.put("typeList",typeList);
        rm.put("locationList",locations);
        rm.put("deptList",depts);
        return rm;
    }

    /**
     * 导出资产调拨列表
     */
    //@RequiresPermissions("eam:allot:export")
    @Log(title = "资产调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamAllotH eamAllotH)
    {
        List<EamAllotH> list = eamAllotHService.selectEamAllotHList(eamAllotH);
        ExcelUtil<EamAllotH> util = new ExcelUtil<EamAllotH>(EamAllotH.class);
        util.exportExcel(response, list, "资产调拨数据");
    }

    /**
     * 获取资产调拨详细信息
     */
    //@RequiresPermissions("eam:allot:query")
    @GetMapping(value = "/{allotId}")
    public AjaxResult getInfo(@PathVariable("allotId") Long allotId)
    {
        return success(eamAllotHService.selectEamAllotHByAllotId(allotId));
    }

    /**
     * 新增资产调拨
     */
    //@RequiresPermissions("eam:allot:add")
    @Log(title = "资产调拨", businessType = BusinessType.INSERT)
    @PostMapping("/addAllotH")
    public AjaxResult addAllotH(@RequestBody EamAllotH eamAllotH)
    {
        System.out.println(eamAllotH);
        //锁
        RLock lock = redissonClient.getLock("myinsertAllotCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamAllotHService.insertEamAllotH(eamAllotH));
            }
        }finally {
            //释放
            lock.unlock();
        }
    }

    //@RequiresPermissions("eam:loanBack:addD")
    @Log(title = "资产变更", businessType = BusinessType.INSERT)
    @PostMapping("/addAllotD")
    public AjaxResult addAllotD(@RequestBody List<EamAsset> eamAssets)
    {
        System.out.println(eamAssets);
        //锁
        RLock lock = redissonClient.getLock("myinsertAllotDCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamAllotDService.inertAllotD(eamAssets,null));
            }
        }finally {
            //释放
            lock.unlock();

        }
    }


    /**
     * 修改资产调拨
     */
    //@RequiresPermissions("eam:allot:edit")
    @Log(title = "资产调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editH(@RequestBody EamAllotH eamAllotH)
    {
        return toAjax(eamAllotHService.updateEamAllotH(eamAllotH));
    }

    //@RequiresPermissions("eam:h:editD")
    @Log(title = "修改:删除调拨从表", businessType = BusinessType.UPDATE)
    @PostMapping("/editD")
    public AjaxResult editD(@RequestBody UpdateEamAllot eamAllot)
    {
        Long id = eamAllot.getEamAllotH().getAllotId();
        //先做删除从表操作
        int num = eamAllotDService.deleteEamAllotDByAssetId(id);
        //在做添加从表操作
        eamAllotDService.inertAllotD(eamAllot.getEamAssets(),eamAllot.getEamAllotH().getAllotId());
        //修改主
        return toAjax(eamAllotHService.updateEamAllotH(eamAllot.getEamAllotH()));
    }

    @Log(title = "修改调拨状态", businessType = BusinessType.UPDATE)
    @PostMapping("editStatus")
    public AjaxResult editStatus(@RequestBody List<EamAllotH> eamAllotHS)
    {
        int num = 0;
        for (EamAllotH eamAllotH : eamAllotHS) {
            num = eamAllotHService.updateEamAllotH(eamAllotH);
        }
        return toAjax(num);
    }

    /**
     * 删除资产调拨
     */
    //@RequiresPermissions("eam:allot:remove")
    @Log(title = "资产调拨", businessType = BusinessType.DELETE)
	@DeleteMapping("/{allotIds}")
    public AjaxResult remove(@PathVariable Long[] allotIds)
    {
        return toAjax(eamAllotHService.deleteEamAllotHByAllotIds(allotIds));
    }
}
